local zhilve = fk.CreateSkill {
  name = "lvshi__zhilve",
  dynamic_desc = function(self, player)
    if player:getTableMark("zhilve_record")[1] == "basic" then
      return "lvshi__zhilve_basic"
    elseif player:getTableMark("zhilve_record")[1] == "nullification" then
      return "lvshi__zhilve_nullfication"
    else
      return "lvshi__zhilve"
    end
  end,
}

Fk:loadTranslationTable{
  ["lvshi__zhilve"] = "知略",
  [":lvshi__zhilve"] = "每回合各限一次，若上一张指定你为目标的牌为：非基本牌，你可将一张牌当任意基本牌使用；基本牌，你可将一张牌当【无懈可击】使用。",
  [":lvshi__zhilve_basic"] = "每回合各限一次，若上一张指定你为目标的牌为：<font color=\"#6495ED\">非基本牌，你可将一张牌当任意基本牌使用；</font>基本牌，你可将一张牌当【无懈可击】使用。",
  [":lvshi__zhilve_nullfication"] = "每回合各限一次，若上一张指定你为目标的牌为：非基本牌，你可将一张牌当任意基本牌使用；<font color=\"#6495ED\">基本牌，你可将一张牌当【无懈可击】使用。</font>",

  ["$lvshi__zhilve1"] = "福祸相依，姑且看之。",
  ["$lvshi__zhilve2"] = "智当用于众人之所不能知之事。",
}

local DIY = require "packages/diy_utility/diy_utility"
local U = require "packages/utility/utility"

zhilve:addEffect("viewas", {
  pattern = ".|.|.|.|.|basic;nullification",
  interaction = function(self, player)
    if #player:getTableMark("zhilve_record") == 0 then return end
    local all_names = {}
    if player:getTableMark("zhilve_record")[1] == "basic" and player:getMark("zhilve_basic-turn") == 0 then
      all_names = Fk:getAllCardNames("b")
    elseif player:getTableMark("zhilve_record")[1] == "nullification" and player:getMark("zhilve_nullification-turn") == 0 then
      table.insert(all_names, "lvshi__nullification")
    end
    local names = player:getViewAsCardNames(zhilve.name, all_names, nil, {})
    if #names == 0 then return end
    return UI.CardNameBox {choices = names, all_choices = all_names}
  end,
  card_filter = Util.TrueFunc,
  view_as = function(self, player, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)
    card.skillName = zhilve.name
    return card
  end,
  enabled_at_response = function(self, player, response)
    return not response
  end,
  before_use = function(self, player, use)
    if use.card.type == Card.TypeBasic then
      player.room:addPlayerMark(player, "zhilve_basic-turn")
    elseif use.card.trueName == "nullification" then
      player.room:addPlayerMark(player, "zhilve_nullification-turn")
    end
  end,
})

zhilve:addEffect(fk.TargetConfirmed, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(zhilve.name) and target == player
  end,
  on_refresh = function(self, event, target, player, data)
    if data.card.type == Card.TypeBasic then
      player.room:setPlayerMark(player, "zhilve_record", {"nullification"})
    else
      player.room:setPlayerMark(player, "zhilve_record", {"basic"})
    end
  end,
})

return zhilve